// 该页面为重置密码页面，已归类到 user 目录
<template>
  <div class="login-bg">
    <div class="login-container">
      <div class="reset-title">修改密码</div>
      <el-form ref="form" :model="param" :rules="changePwdFormRules" size="large">
        <!--用户名-->
        <el-form-item prop="username">
          <el-input v-model="param.username" placeholder="用户名">
            <template #prepend>
              <el-icon>
                <User />
              </el-icon>
            </template>
          </el-input>
        </el-form-item>
        <!--验证码-->
        <el-form-item prop="code">
          <el-input v-model="param.code" placeholder="验证码">
            <template #prepend>
              <el-icon>
                <Message />
              </el-icon>
            </template>
            <template #suffix>
              <el-button
                :type="sendButtonType"
                :disabled="sendButtonDisabled"
                @click="handleSendCode"
              >
                发送
                <span v-if="countdown > 0">({{ countdown }})</span>
              </el-button>
            </template>
          </el-input>
        </el-form-item>

        <!-- 新密码-->
        <el-form-item prop="password">
          <el-input v-model="param.password" type="password" show-password placeholder="新密码">
            <template #prepend>
              <el-icon>
                <User />
              </el-icon>
            </template>
          </el-input>
        </el-form-item>
        <!-- 确认密码-->
        <el-form-item prop="confirmPassword">
          <el-input
            v-model="param.confirmPassword"
            type="password"
            show-password
            placeholder="新密码"
          >
            <template #prepend>
              <el-icon>
                <User />
              </el-icon>
            </template>
          </el-input>
        </el-form-item>

        <el-button
          class="login-btn"
          type="primary"
          size="large"
          @click="submitForm(form, changePassword)"
        >
          确认
        </el-button>
        <p class="login-text">
          <el-link type="primary" @click="goToLogin">返回登录</el-link>
        </p>
      </el-form>
    </div>
  </div>
</template>

<script setup lang="ts">
  import { computed, reactive, ref } from 'vue'
  import { ElMessage, type FormInstance, type FormRules } from 'element-plus'
  import { Message, User } from '@element-plus/icons-vue'
  import { sendVCode } from '@/api/common.ts'
  import { submitForm, validate } from '@/utils/validate.ts'
  import { passwordReset } from '@/api/user.ts'
  import { ResetPwdParam } from '@/types'
  import { useParamsStore } from '@/store/params.ts'
  import router from '@/router'

  const paramsStore = useParamsStore()
  //发送验证码按钮倒计时
  const countdown = ref(0)
  const startCountdown = () => {
    const timer = setInterval(() => {
      countdown.value--
      if (countdown.value < 0) {
        clearInterval(timer)
      }
    }, 1000)
    return () => clearInterval(timer)
  }

  //发送验证码方法
  const handleSendCode = async () => {
    const sendVCodeParam = { username: param.username }
    //验证请求参数
    validate(sendVCodeParam, { username: [{ required: true, message: '请输入用户名' }] })
    await sendVCode(sendVCodeParam)
    //发送成功
    countdown.value = 60
    startCountdown()
  }
  //发送按钮类型
  const sendButtonType = computed(() => {
    return countdown.value > 0 ? 'info' : 'primary'
  })
  //发送按钮禁用状态
  const sendButtonDisabled = computed(() => {
    return countdown.value > 0
  })

  //修改密码参数
  const param = reactive<ResetPwdParam>({
    username: '',
    password: '',
    confirmPassword: '',
    code: '',
  })
  param.username = paramsStore.username

  //修改密码表单规则
  const confirmPasswordValidator = (
    _rule: unknown,
    value: string,
    callback: (_error?: Error) => void
  ) => {
    if (!value) {
      callback(new Error('请重复密码'))
    } else if (value !== param.password) {
      callback(new Error('两次输入密码不同'))
    } else {
      callback()
    }
  }
  const changePwdFormRules: FormRules = {
    username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
    password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
    confirmPassword: [{ validator: confirmPasswordValidator, trigger: 'blur' }],
    code: [{ required: true, message: '请输入验证码', trigger: 'blur' }],
  }

  const form = ref<FormInstance>()
  //修改密码方法
  const changePassword = async () => {
    await passwordReset(param)
    ElMessage.success('修改成功')
    goToLogin()
  }

  //跳登陆页方法
  const goToLogin = () => {
    //保存当前输入的用户名
    paramsStore.handleSetUsername(param.username)
    router.push('/login')
  }
</script>

<style scoped>
  .login-bg {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 100vh;
    background: url(@/assets/img/login-bg.jpg) center/cover no-repeat;
  }

  .reset-title {
    text-align: center;
    font-size: 22px;
    color: #333;
    font-weight: bold;
    margin-bottom: 10px;
  }

  .login-container {
    width: 450px;
    border-radius: 5px;
    background: #fff;
    padding: 40px 50px 50px;
    box-sizing: border-box;
  }

  .login-btn {
    display: block;
    width: 100%;
  }

  .login-text {
    display: flex;
    align-items: center;
    justify-content: center;
    margin-top: 20px;
    font-size: 14px;
    color: #333;
  }

  /*noinspection CssUnusedSymbol*/
  :deep(.el-input .el-input__wrapper:has(button)) {
    padding-right: 0;
  }
</style>
